//
//  FirstViewController.m
//  SinaWebo
//
//  Created by 亦心 on 16/7/13.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "FirstViewController.h"
#import "CardlistHeadCard.h"
#import "DiscoverService.h"
#import "CardlistInfo.h"
#import "ChannelList.h"
#import "CircleView.h"
#import "Picitem.h"
#import "LiveCard.h"
#import "LivePicItem.h"
#import "DiscoverViewCell.h"
#import "FirstTopViewCell.h"
#import "FirstCenterViewCell.h"
#import "CardGroup.h"

@interface FirstViewController ()
@property (weak, nonatomic) CircleView *circleView;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation FirstViewController
static NSString *const DIS_CELL_ID = @"DiscoverViewCell";
static NSString *const HEADER_ID = @"FirstTopViewCell";
static NSString *const CENTER_ID = @"FirstCenterViewCell";

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MyColor(244, 244, 244);
    self.tableView.sectionFooterHeight = 0.0;
    self.tableView.sectionHeaderHeight = 10.0;
    self.tableView.contentInset = UIEdgeInsetsMake(164, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self registXib];
    [self addCircleView];
    
    [self getLiveListData];
}

- (void)addCircleView{
    CircleView *circleView = [CircleView circleView];
    circleView.frame = CGRectMake(0, -60, kScreenW, 100);
    [self.tableView addSubview:circleView];
    self.circleView = circleView;
}

- (void)registXib{
    [self.tableView registerClass:[DiscoverViewCell class] forCellReuseIdentifier:DIS_CELL_ID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FirstTopViewCell class]) bundle:nil] forCellReuseIdentifier:HEADER_ID];
    [self.tableView registerClass:[FirstCenterViewCell class] forCellReuseIdentifier:CENTER_ID];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LiveCard *card = _dataArray[indexPath.row];
    if (card.card_type.integerValue == 17) {
        return 88;
    }else if (card.card_type.integerValue == 42){
        return 40;
    }else if (card.card_type.integerValue == 11){
        return 590;
    }
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LiveCard *card = _dataArray[indexPath.row];
    if (card.card_type.integerValue == 17) {
        DiscoverViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DIS_CELL_ID forIndexPath:indexPath];
        cell.dataArray = card.group;
        return cell;
    }else if (card.card_type.integerValue == 42){
        FirstTopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HEADER_ID forIndexPath:indexPath];
        cell.desc = card.desc;
        return cell;
    } else if (card.card_type.integerValue == 11){
        FirstCenterViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CENTER_ID forIndexPath:indexPath];
        cell.dataArray = [self getCenterArray:indexPath];
        return cell;
    } else{
        return [[UITableViewCell alloc] init];
    }
    
}

- (NSArray *)getCenterArray:(NSIndexPath *)indexpath{
    NSMutableArray *array = [NSMutableArray array];
    LiveCard *card = _dataArray[indexpath.row];
    for (CardGroup *group in card.card_group) {
//        [array addObject:group.left_element];
//        [array addObject:group.right_element];
    }
    return [array copy];
}

- (void)getLiveListData{
    LiveListRequest *request = [LiveListRequest request];
    request.fid = @"2310750001";
    request.lcardid = @"1001000002_548_-_MOBILE_SQUARE_231075";
    request.containerid = @"2310750001";
    request.luicode = @"10000010";
    request.lfid = @"1001001";
    request.uicode = @"10000011";
    request.sourcetype = @"page";
    [DiscoverService getLiveListWithRequest:request success:^(LiveListResponse *response) {
        self.dataArray = response.cards;
        [self setupHeaderData:self.dataArray.firstObject];
        [self.tableView reloadData];
    } error:nil];
}

- (void)setupHeaderData:(LiveCard *)card{
    NSMutableArray *images = [NSMutableArray array];
    NSArray *picItems = card.pic_items;
    if (picItems.count) {
        for (LivePicItem *picItem in picItems) {
            [images addObject:picItem.pic];
        }
    }
    self.circleView.dataArray = images;
}


@end
